import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { productApi } from "@/api/erp/drug/drug";
import { message } from "antd";
import { useTranslation } from "react-i18next";
import { useState, useMemo } from "react";
let reload: Function;
let productId: any;
const index = (props: { register: Fn }) => {
	const [isUpdated, setIsUpdated] = useState(false);
	const { t } = useTranslation();
	const en_vi = "drugStorage.equipment.device";
	const formSchema: FormSchema[] = [
		{
			label: t(`${en_vi}.eqpName`),
			field: "eqpName",
			component: "Input"
		},
		{
			label: t(`${en_vi}.eqpImage`),
			field: "eqpImage",
			component: "FileUpload",
			componentProps: {
				fileType: "image",
				maxCount: 1
			}
		},
		{
			label: t(`${en_vi}.genVno`),
			field: "genVno",
			component: "Input"
		},
		{
			label: t(`${en_vi}.orgId`),
			field: "orgId",
			component: "Input"
		},
		// {
		// 	label: t(`${en_vi}.eqpType`),
		// 	field: "eqpType",
		// 	component: "Input"
		// },
		{
			label: t(`${en_vi}.proDtime`),
			field: "proDtime",
			component: "DatePicker"
		},
		{
			label: t(`${en_vi}.buyDtime`),
			field: "buyDtime",
			component: "DatePicker"
		},
		{
			label: t(`${en_vi}.expDtime`),
			field: "expDtime",
			component: "DatePicker"
		},
		{
			label: t(`${en_vi}.qltyGuarFrom`),
			field: "qltyGuarFrom",
			component: "DatePicker"
		},
		{
			label: t(`${en_vi}.qltyGuarTo`),
			field: "qltyGuarTo",
			component: "DatePicker"
		},
		{
			label: t(`${en_vi}.catOuter`),
			field: "catOuter",
			component: "Input"
		},
		{
			label: t(`${en_vi}.catInner`),
			field: "catInner",
			component: "Input"
		},
		{
			label: t(`${en_vi}.subClass`),
			field: "subClass",
			component: "Input"
		},
		{
			label: t(`${en_vi}.shelfSq`),
			field: "shelfSq",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.unitPrice`),
			field: "unitPrice",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.unitName`),
			field: "unitName",
			component: "Input"
		},
		{
			label: t(`${en_vi}.smktPrice`),
			field: "smktPrice",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.umktPrice`),
			field: "umktPrice",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.einvoiceTag`),
			field: "einvoiceTag",
			component: "RadioGroup",
			componentProps: {
				options: [
					{ label: t(`drugStorage.yes`), value: "1" },
					{ label: t(`drugStorage.no`), value: "0" }
				]
			}
		},
		{
			label: t(`${en_vi}.invoiceTag`),
			field: "invoiceTag",
			component: "RadioGroup",
			componentProps: {
				options: [
					{ label: t(`drugStorage.yes`), value: "1" },
					{ label: t(`drugStorage.no`), value: "0" }
				]
			}
		},
		{
			label: t(`${en_vi}.invoiceTaxRate`),
			field: "invoiceTaxRate",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.invoiceTinSno`),
			field: "invoiceTinSno",
			component: "Input"
		}
		// {
		// 	label: t("felix_ui.status"),
		// 	component: "Select",
		// 	field: "status",
		// 	componentProps: {
		// 		options: [
		// 			{ label: t(`drugStorage.notStarted`), value: "-1" },
		// 			{ label: t(`drugStorage.inApproval`), value: "1" },
		// 			{ label: t(`drugStorage.approved`), value: "2" },
		// 			{ label: t(`drugStorage.approvalNotApproved`), value: "3" },
		// 			{ label: t(`drugStorage.cancelled`), value: "4" }
		// 		]
		// 	}
		// }
	];
	const getTitle = useMemo(() => {
		return isUpdated ? t(`felix_ui.edit`) : t(`felix_ui.add`);
	}, [isUpdated]);
	const [register, { validateFields, setFieldsValue, resetFields }] = useForm({
		schemas: formSchema,
		labelWidth: 120,
		baseColProps: {
			span: 24
		}
	});
	const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(props.register, data => {
		reload = data.reload;
		productId = data.record?.productId;
		resetFields();
		setDrawerProps({ confirmLoading: true });
		setIsUpdated(!!data?.isUpdated);
		setFieldsValue({
			...data.record
		});
		setDrawerProps({ confirmLoading: false });
	});
	const handleSubmit = async () => {
		const formValue = await validateFields();
		formValue.proDtime = new Date(formValue.proDtime).getTime();
		formValue.buyDtime = new Date(formValue.buyDtime).getTime();
		formValue.expDtime = new Date(formValue.expDtime).getTime();
		formValue.qltyGuarFrom = new Date(formValue.qltyGuarFrom).getTime();
		formValue.qltyGuarTo = new Date(formValue.qltyGuarTo).getTime();
		formValue.equipments = { ...formValue, eqpId: productId };
		formValue.bizType = "3";
		formValue.productName = formValue.eqpName;
		let res;
		if (isUpdated) {
			formValue.productId = productId;
			res = await productApi.updateproduct(formValue);
		} else {
			res = await productApi.createproduct(formValue);
		}
		if (!res) {
			message.error(t("viewComponents.patientInformation.additionFailed"));
			return;
		}
		message.success(t("viewComponents.patientInformation.successfullyAdded"));
		closeDrawer();
		reload();
	};
	return (
		<BasicDrawer title={getTitle} showFooter={true} {...props} register={registerDrawer} ok={handleSubmit}>
			<BasicForm register={register} labelWrap={true}></BasicForm>
		</BasicDrawer>
	);
};
export default index;
